package common.customer.action;

import java.util.List;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import common.customer.bean.Collect;
import common.customer.bean.Customer;
import common.customer.service.CollectService;
import common.customer.service.CustomerService;
import common.util.StringUtil;
import common.util.StrutsUtil;
import common.util.mail.MailSender;

public class CustomerAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private CustomerService customerService;
	private CollectService collectService; 
	// 用户名
	private String userName;
	private String email;
	private String validationCode;
	private String resultStr;
	private String resultCode;
	private String uuid;
	private Customer customer;
	private String passWord;
	private String passWord2;
	private int pageSize;
	private int total;
	private int totalPage;
	private int page;
	private List<Collect> collects;
	private String nicheng;
	private String xinqing;
	public String qiandao(){
		customer = (Customer)ServletActionContext.getRequest().getSession().getAttribute("user");
		if(customer==null){
			resultCode = "0";
			resultStr = "用户尚未登录!";
			return SUCCESS;
		}
		if(xinqing==null || "".equals(xinqing.trim())){
			resultCode = "0";
			resultStr = "心情不能为空!";
			return SUCCESS;
		}
		int huodejifen = this.customerService.qiandao(customer.getId(), xinqing);
		if(huodejifen>0){
			resultCode = "1";
			resultStr = "签到成功,获得"+huodejifen+"积分!";
			return SUCCESS;
		}else{
			resultCode = "1";
			resultStr = "今日已经签到,不能重复获得积分!";
			return SUCCESS;
		}
	}
	public String changeNicheng(){
		customer = (Customer)ServletActionContext.getRequest().getSession().getAttribute("user");
		if(customer==null){
			resultCode = "0";
			resultStr = "用户尚未登录!";
			return SUCCESS;
		}
		if(nicheng==null || "".equals(nicheng.trim())){
			resultCode = "0";
			resultStr = "新的昵称不能为空!";
			return SUCCESS;
		}
		if(this.customerService.testNichengExist(customer.getId(), nicheng)){
			resultCode = "0";
			resultStr = "昵称已经有人使用,请换一个昵称!";
			return SUCCESS;
		}
		if(this.customerService.updateNicheng(customer.getId(), nicheng)){
			resultCode = "1";
			resultStr = "修改昵称成功!";
			return SUCCESS;
		}else{
			resultCode = "0";
			resultStr = "修改昵称失败!";
			return SUCCESS;
		}
		
	}
	public String generateNewPassword(){
		if(!StrutsUtil.validationCodeIsRight(ServletActionContext.getRequest().getSession(), validationCode)){
			resultCode = "0";
			resultStr = "验证码已过期,请换一个验证码!";
			return SUCCESS;
		}
		if(uuid==null || "".equals(uuid.trim())){
			resultCode = "0";
			resultStr = "链接已经找不到!";
			return SUCCESS;
		}else if(StringUtil.isBlank(passWord) || StringUtil.isBlank(passWord2) || !passWord.trim().equals(passWord2.trim())){
			resultCode = "0";
			resultStr = "两次输入的密码不一致!";
			return SUCCESS;
		}else{
			customer = this.customerService.getCustomerByUUID(uuid);
			if(System.currentTimeMillis()-customer.getUuidGenerateTime()>7200*1000L){
				resultCode = "0";
				resultStr = "已过期!";
				return SUCCESS;
			}
			
			
			customer.setPassWord(passWord);
			customer.setUuid(null);
			customer.setUuidGenerateTime(0);
			this.customerService.updateCustomer(customer);
			resultCode = "1";
			resultStr = "新密码设置成功,请登录!";
			return SUCCESS;
		}
	}
	public String linkLookPassword(){
		if(uuid==null || "".equals(uuid.trim())){
			resultCode = "0";
			resultStr = "链接已经找不到!";
			return "linknotfound";
		}else{
			customer = this.customerService.getCustomerByUUID(uuid);
			if(customer==null){
				resultCode = "0";
				resultStr = "链接已经找不到!";
				return "linknotfound";
			}else{
				if(System.currentTimeMillis()-customer.getUuidGenerateTime()>7200*1000L){
					resultCode = "0";
					resultStr = "已过期!";
					return "linknotfound";
				}
				return SUCCESS;
			}
		}
	}
	
	public String lookPassword(){
		if (userName == null || email == null || "".equals(userName.trim())
				|| "".equals(email.trim())) {
			resultCode = "0";
			resultStr = "用户名邮箱都不能为空!";
			return SUCCESS;
		}
		if (validationCode == null || "".equals(validationCode.trim())) {
			resultCode = "0";
			resultStr = "验证码不能为空!";
			return SUCCESS;
		}
		String validationCodeSess = ServletActionContext.getRequest()
				.getSession().getAttribute("validationCode") == null ? null
				: ServletActionContext.getRequest().getSession()
						.getAttribute("validationCode").toString().trim();
		if (!validationCode.equals(validationCodeSess)) {
			resultCode = "0";
			resultStr = "验证码数据错误!";
			return SUCCESS;
		}
		
		Customer customer = this.customerService.getCustomerByName(userName); 
		if(customer==null){
			resultCode = "0";
			resultStr = "系统没有此用户!";
			return SUCCESS;
		}else{
			if(email.equals(customer.getEmail())){
				String uuid = UUID.randomUUID().toString();
				customer.setUuid(uuid);
				customer.setUuidGenerateTime(System.currentTimeMillis());
				
				boolean updateUser = this.customerService.updateCustomer(customer);
				if(updateUser){
					MailSender.sendLookPassword(customer.getEmail(), uuid);
				}
				resultCode = "1";
				resultStr = "修改密码接口已经发到您的邮箱,请在两个小时内修改新密码!";
				return SUCCESS;
			}else{
				resultCode = "0";
				resultStr = "输入的邮箱和你注册时的邮箱不一致!";
				return SUCCESS;
			}
		}
	}
	
	public String mainPage(){
		Object user = ServletActionContext.getRequest().getSession().getAttribute("user");
		if(user == null){
			return "notlogin";
		}else{
			customer = this.customerService.getCustomerByID(((Customer)user).getId());
			return "login";
		}
	}
	public String ucenterCollection(){
		Object user = ServletActionContext.getRequest().getSession().getAttribute("user");
		if(user == null){
			return "notlogin";
		}
		Customer cust = (Customer)user;
		//获得总量
		total = (int)this.collectService.getCollectCountByCustomerId(cust.getId());
		
		if(pageSize <= 0){
			pageSize=20;
		}
		totalPage = total%pageSize==0?total/pageSize:total/pageSize+1;
		if(page<1){
			page=1;
		}
		if(page>totalPage){
			page=totalPage;
		}
		//获得列表
		collects = this.collectService.getCollectByCustomerId(cust.getId(), page, pageSize);
		return "login";
	}
	
	public String ucenterIndex(){
		Object user = ServletActionContext.getRequest().getSession().getAttribute("user");
		if(user == null){
			return "notlogin";
		}else{
			customer = this.customerService.getCustomerByID(((Customer)user).getId());
			return "login";
		}
	}
	public String ucenterAccount(){
		Object user = ServletActionContext.getRequest().getSession().getAttribute("user");
		customer = (Customer)(user);
		if(user == null){
			return "notlogin";
		}else{
			return "login";
		}
	}
	public String ucenterCredit(){
		Object user = ServletActionContext.getRequest().getSession().getAttribute("user");
		if(user == null){
			return "notlogin";
		}else{
			return "login";
		}
	}
	public String ucenterInvite(){
		Object user = ServletActionContext.getRequest().getSession().getAttribute("user");
		if(user == null){
			return "notlogin";
		}else{
			return "login";
		}
	}
	public String ucenterPublish(){
		Object user = ServletActionContext.getRequest().getSession().getAttribute("user");
		if(user == null){
			return "notlogin";
		}else{
			return "login";
		}
	}
	
	
	
	public String logout(){
		ServletActionContext.getRequest().getSession().removeAttribute("user");
		resultCode = "1";
		resultStr = "成功退出";
		return SUCCESS;
	}
	// =================getter and setter method==================
	public String getResultStr() {
		return resultStr;
	}
	public void setResultStr(String resultStr) {
		this.resultStr = resultStr;
	}
	public String getResultCode() {
		return resultCode;
	}
	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	public CustomerService getCustomerService() {
		return customerService;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getValidationCode() {
		return validationCode;
	}
	public void setValidationCode(String validationCode) {
		this.validationCode = validationCode;
	}
	public String getUuid() {
		return uuid;
	}
	public void setUuid(String uuid) {
		this.uuid = uuid;
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	public String getPassWord() {
		return passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	public String getPassWord2() {
		return passWord2;
	}
	public void setPassWord2(String passWord2) {
		this.passWord2 = passWord2;
	}
	public CollectService getCollectService() {
		return collectService;
	}
	public void setCollectService(CollectService collectService) {
		this.collectService = collectService;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public List<Collect> getCollects() {
		return collects;
	}
	public void setCollects(List<Collect> collects) {
		this.collects = collects;
	}
	public String getNicheng() {
		return nicheng;
	}
	public void setNicheng(String nicheng) {
		this.nicheng = nicheng;
	}
	public String getXinqing() {
		return xinqing;
	}
	public void setXinqing(String xinqing) {
		this.xinqing = xinqing;
	}
}
